package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {


    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;






    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        LambdaQueryWrapper<BaseCategoryTrademark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(wrapper);

        if (baseCategoryTrademarks != null && baseCategoryTrademarks.size() > 0) {

            List<Long> collect = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            return baseTrademarkMapper.selectBatchIds(collect);
        }

        return null;
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //获取到品牌id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //判断不能为空进行下面的操作
        if (trademarkIdList != null || trademarkIdList.size() > 0) {
            //stream流map方法转换collect集合进行添加
            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map(trademarkId -> {
                //创建一个分类id与品牌表关联的对象 ,把里面的值进行添加
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                //返回map转换的数组
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //集合中保存数据
            this.saveBatch(collect);
        }

    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        //哪些是关联品牌的id
        LambdaQueryWrapper<BaseCategoryTrademark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(wrapper);
        List<BaseTrademark> baseTrademarks = baseTrademarkMapper.selectList(null);
        if (baseCategoryTrademarks != null && baseCategoryTrademarks.size() > 0) {
            //找到关联品牌的id集合{1,2}
            List<Long> ids = baseCategoryTrademarks.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //在所有的品牌id中将这些有关联的品牌id给过滤掉就可以

            List<BaseTrademark> collect = baseTrademarks.stream().filter(baseTrademark -> {
                return !ids.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            return collect;
        }

        return baseTrademarks;
    }

    @Override
    public void remove(Long category3Id, Long trademarkId) {

        LambdaQueryWrapper<BaseCategoryTrademark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        wrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        baseCategoryTrademarkMapper.delete(wrapper);

    }
}
